﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;


namespace TeacherHelp.FormList.Admin
{
    public partial class UserEditForm : BanFlashingForm
    {

        private readonly AdminMainForm _parentForm;

        private readonly int _userId;

        public UserEditForm()
        {
            InitializeComponent();
        }

        public UserEditForm(AdminMainForm parent, int userId) : this()
        {
            _parentForm = parent;
            _userId = userId;
        }

        private void UserAddForm_Load(object sender, EventArgs e)
        {
            //用户数据查询
            InitData();
        }

        private void InitData()
        {
            BLL.UserManager userManager = new BLL.UserManager();
            var data = userManager.GetUserByUserId(_userId);

            this.loginNameTxt.Text = data.LoginName;
            this.nameTxt.Text = data.Name;

            //this.oldPwdTxt.Enabled = false;

            if (data.Sex == "男")
            {
                this.boyradio.Checked = true;
            }
            else
            {
                this.girlradio.Checked = true;
            }

            //类型下拉框初始化
            typeComboxDataInit(data.roleName);
            //班级下拉框初始化
            groupComboxDataInit(data.gradeName);

        }

        private void saveBtn_Click(object sender, EventArgs e)
        {
            ViewModel.User user = new ViewModel.User();
            user.Id = _userId;
            user.LoginName = this.loginNameTxt.Text.Trim();
            user.LoginPwd = this.pwdTxt.Text.Trim();
            user.Name = this.nameTxt.Text.Trim();
            user.Sex = this.boyradio.Checked ? "男" : "女";
            user.RoleId = this.typeComBox.SelectedValue.ToInt();
            user.gradeId = this.groupComBox.SelectedValue.ToInt();
            user.roleName = this.typeComBox.Text.Trim();

            if (string.IsNullOrEmpty(user.LoginName))
            {
                MessageBox.Show("用户姓名不允许为空！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            if (string.IsNullOrEmpty(user.LoginPwd))
            {
                MessageBox.Show("密码不允许为空！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            if (string.IsNullOrEmpty(user.Name))
            {
                MessageBox.Show("姓名不允许为空！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            BLL.UserManager userManager = new BLL.UserManager();
            string msg = userManager.EditUser(user);

            if (MessageBox.Show(msg, "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk) == DialogResult.OK)
            {
                _parentForm.ShowUserInfoForm();
            }

        }

        private void groupComboxDataInit(string gradeName)
        {
            BLL.GradeManager gradeManager = new BLL.GradeManager();
            var data = gradeManager.GetGradeAllList();
            data.Insert(0, new Model.Grade { Name = "未设置", Id = -1 });
            this.groupComBox.DataSource = data;
            this.groupComBox.DisplayMember = nameof(Model.Grade.Name);
            this.groupComBox.ValueMember = nameof(Model.Grade.Id);

            var index = this.groupComBox.FindString(gradeName);
            this.groupComBox.SelectedIndex = index != -1 ? index : 0;
        }

        private void typeComboxDataInit(string roleName)
        {
            if (roleName == "超级管理员")
            {
                this.typeComBox.Enabled = false;
            }

            BLL.RoleManager roleManager = new BLL.RoleManager();
            var data = roleManager.GetRoleAllList(roleName == "超级管理员");
            this.typeComBox.DataSource = data;
            this.typeComBox.DisplayMember = nameof(Model.Role.Name);
            this.typeComBox.ValueMember = nameof(Model.Role.Id);
            //throw new NotImplementedException();
            var index = this.typeComBox.FindString(roleName);
            this.typeComBox.SelectedIndex = index != -1 ? index : 0;

        }

        private void cancelBtn_Click(object sender, EventArgs e)
        {
            _parentForm.ShowUserInfoForm();
        }

    }
}
